﻿/**  版本信息模板在安装目录下，可自行修改。
* hars_reportlist.cs
*
* 功 能： N/A
* 类 名： hars_reportlist
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2018/1/20 11:54:27   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;

namespace PreeRegisterDAL
{
    /// <summary>
    /// 数据访问类:hars_reportlist
    /// </summary>
    public partial class hars_reportlist
    {
        public hars_reportlist()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("id", "hars_reportlist");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from hars_reportlist");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@id", MySqlDbType.Int32)
            };
            parameters[0].Value = id;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(PreeRegisterModel.hars_reportlist model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into hars_reportlist(");
            strSql.Append("hospid,hospname,deptid,deptname,pid,repid,repnum,title,otime,docname,time,type,typeid,synstae,synmsg,isdelete,createdate,lastdate)");
            strSql.Append(" values (");
            strSql.Append("@hospid,@hospname,@deptid,@deptname,@pid,@repid,@repnum,@title,@otime,@docname,@time,@type,@typeid,@synstae,@synmsg,@isdelete,@createdate,@lastdate)");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@hospid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@hospname", MySqlDbType.VarChar,200),
                    new MySqlParameter("@deptid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@deptname", MySqlDbType.VarChar,200),
                    new MySqlParameter("@pid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@repid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@repnum", MySqlDbType.VarChar,200),
                    new MySqlParameter("@title", MySqlDbType.VarChar,2000),
                    new MySqlParameter("@otime", MySqlDbType.VarChar,200),
                    new MySqlParameter("@docname", MySqlDbType.VarChar,200),
                    new MySqlParameter("@time", MySqlDbType.VarChar,200),
                    new MySqlParameter("@type", MySqlDbType.VarChar,200),
                    new MySqlParameter("@typeid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@synstae", MySqlDbType.Int32,11),
                    new MySqlParameter("@synmsg", MySqlDbType.VarChar,2000),
                    new MySqlParameter("@isdelete", MySqlDbType.Int32,2),
                    new MySqlParameter("@createdate", MySqlDbType.Timestamp),
                    new MySqlParameter("@lastdate", MySqlDbType.Timestamp)};
            parameters[0].Value = model.hospid;
            parameters[1].Value = model.hospname;
            parameters[2].Value = model.deptid;
            parameters[3].Value = model.deptname;
            parameters[4].Value = model.pid;
            parameters[5].Value = model.repid;
            parameters[6].Value = model.repnum;
            parameters[7].Value = model.title;
            parameters[8].Value = model.otime;
            parameters[9].Value = model.docname;
            parameters[10].Value = model.time;
            parameters[11].Value = model.type;
            parameters[12].Value = model.typeid;
            parameters[13].Value = model.synstae;
            parameters[14].Value = model.synmsg;
            parameters[15].Value = model.isdelete;
            parameters[16].Value = model.createdate;
            parameters[17].Value = model.lastdate;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PreeRegisterModel.hars_reportlist model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update hars_reportlist set ");
            strSql.Append("hospid=@hospid,");
            strSql.Append("hospname=@hospname,");
            strSql.Append("deptid=@deptid,");
            strSql.Append("deptname=@deptname,");
            strSql.Append("pid=@pid,");
            strSql.Append("repid=@repid,");
            strSql.Append("repnum=@repnum,");
            strSql.Append("title=@title,");
            strSql.Append("otime=@otime,");
            strSql.Append("docname=@docname,");
            strSql.Append("time=@time,");
            strSql.Append("type=@type,");
            strSql.Append("typeid=@typeid,");
            strSql.Append("synstae=@synstae,");
            strSql.Append("synmsg=@synmsg,");
            strSql.Append("isdelete=@isdelete");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@hospid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@hospname", MySqlDbType.VarChar,200),
                    new MySqlParameter("@deptid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@deptname", MySqlDbType.VarChar,200),
                    new MySqlParameter("@pid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@repid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@repnum", MySqlDbType.VarChar,200),
                    new MySqlParameter("@title", MySqlDbType.VarChar,2000),
                    new MySqlParameter("@otime", MySqlDbType.VarChar,200),
                    new MySqlParameter("@docname", MySqlDbType.VarChar,200),
                    new MySqlParameter("@time", MySqlDbType.VarChar,200),
                    new MySqlParameter("@type", MySqlDbType.VarChar,200),
                    new MySqlParameter("@typeid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@synstae", MySqlDbType.Int32,11),
                    new MySqlParameter("@synmsg", MySqlDbType.VarChar,2000),
                    new MySqlParameter("@isdelete", MySqlDbType.Int32,2),
                    new MySqlParameter("@id", MySqlDbType.Int32,50)};
            parameters[0].Value = model.hospid;
            parameters[1].Value = model.hospname;
            parameters[2].Value = model.deptid;
            parameters[3].Value = model.deptname;
            parameters[4].Value = model.pid;
            parameters[5].Value = model.repid;
            parameters[6].Value = model.repnum;
            parameters[7].Value = model.title;
            parameters[8].Value = model.otime;
            parameters[9].Value = model.docname;
            parameters[10].Value = model.time;
            parameters[11].Value = model.type;
            parameters[12].Value = model.typeid;
            parameters[13].Value = model.synstae;
            parameters[14].Value = model.synmsg;
            parameters[15].Value = model.isdelete;
            parameters[16].Value = model.id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from hars_reportlist ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@id", MySqlDbType.Int32)
            };
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from hars_reportlist ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PreeRegisterModel.hars_reportlist GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,hospid,hospname,deptid,deptname,pid,repid,repnum,title,otime,docname,time,type,typeid,synstae,synmsg,isdelete,createdate,lastdate from hars_reportlist ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@id", MySqlDbType.Int32)
            };
            parameters[0].Value = id;

            PreeRegisterModel.hars_reportlist model = new PreeRegisterModel.hars_reportlist();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PreeRegisterModel.hars_reportlist DataRowToModel(DataRow row)
        {
            PreeRegisterModel.hars_reportlist model = new PreeRegisterModel.hars_reportlist();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["hospid"] != null)
                {
                    model.hospid = row["hospid"].ToString();
                }
                if (row["hospname"] != null)
                {
                    model.hospname = row["hospname"].ToString();
                }
                if (row["deptid"] != null)
                {
                    model.deptid = row["deptid"].ToString();
                }
                if (row["deptname"] != null)
                {
                    model.deptname = row["deptname"].ToString();
                }
                if (row["pid"] != null)
                {
                    model.pid = row["pid"].ToString();
                }
                if (row["repid"] != null)
                {
                    model.repid = row["repid"].ToString();
                }
                if (row["repnum"] != null)
                {
                    model.repnum = row["repnum"].ToString();
                }
                if (row["title"] != null)
                {
                    model.title = row["title"].ToString();
                }
                if (row["otime"] != null)
                {
                    model.otime = row["otime"].ToString();
                }
                if (row["docname"] != null)
                {
                    model.docname = row["docname"].ToString();
                }
                if (row["time"] != null)
                {
                    model.time = row["time"].ToString();
                }
                if (row["type"] != null)
                {
                    model.type = row["type"].ToString();
                }
                if (row["typeid"] != null)
                {
                    model.typeid = row["typeid"].ToString();
                }
                if (row["synstae"] != null && row["synstae"].ToString() != "")
                {
                    model.synstae = int.Parse(row["synstae"].ToString());
                }
                if (row["synmsg"] != null)
                {
                    model.synmsg = row["synmsg"].ToString();
                }
                if (row["isdelete"] != null && row["isdelete"].ToString() != "")
                {
                    model.isdelete = int.Parse(row["isdelete"].ToString());
                }
                if (row["createdate"] != null && row["createdate"].ToString() != "")
                {
                    model.createdate = DateTime.Parse(row["createdate"].ToString());
                }
                if (row["lastdate"] != null && row["lastdate"].ToString() != "")
                {
                    model.lastdate = DateTime.Parse(row["lastdate"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,hospid,hospname,deptid,deptname,pid,repid,repnum,title,otime,docname,time,type,typeid,synstae,synmsg,isdelete,createdate,lastdate ");
            strSql.Append(" FROM hars_reportlist ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM hars_reportlist ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperMySQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from hars_reportlist T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "hars_reportlist";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        /// <summary>
        /// 获取要推送检验报告的患者PID 绑定用户
        /// </summary>
        /// <param name="hospid">医院ID</param>
        /// <returns></returns>
        public DataSet GetParentList(string hospid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from hars_healthuserbind where 1=1 and bindstate=1 ");
            if (hospid.Trim() != "")
            {
                strSql.AppendFormat(" and hospid='{0}' ", hospid);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 查询检验报告条目
        /// </summary>
        /// <param name="pid">患者ID</param>
        /// <param name="repid">条目ID</param>
        /// <returns></returns>
        public DataSet GetList(string pid, string repid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,hospid,hospname,deptid,deptname,pid,repid,repnum,title,otime,docname,time,type,typeid,synstae,synmsg,isdelete,createdate,lastdate ");
            strSql.Append(" FROM hars_reportlist where 1=1 ");
            if (!string.IsNullOrEmpty(pid))
            {
                strSql.AppendFormat(" and pid='{0}'", pid);
            }
            if (!string.IsNullOrEmpty(repid))
            {
                strSql.AppendFormat(" and repid='{0}'", repid);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 回写发送code和msg
        /// </summary>
        /// <param name="pid">患者ID</param>
        /// <param name="repid">检验报告ID</param>
        /// <param name="code">code</param>
        /// <param name="msg">msg</param>
        public bool Update(string pid, string repid, string code, string msg)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update hars_reportlist set ");
            strSql.Append("synstae=@synstae,");
            strSql.Append("synmsg=@synmsg ");
            strSql.Append(" where pid=@pid and repid=@repid");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@synstae", MySqlDbType.Int32,11),
                    new MySqlParameter("@synmsg", MySqlDbType.VarChar,2000),
                    new MySqlParameter("@pid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@repid", MySqlDbType.VarChar,200)};
            parameters[0].Value = code;
            parameters[1].Value = msg;
            parameters[2].Value = pid;
            parameters[3].Value = repid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion  ExtensionMethod
    }
}

